/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of libfixed.

    libfixed is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libfixed is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libfixed; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifdef DEBUG
#include <math.h>
#include <stdio.h>
#endif

#include "fixed.h"

int pow10i(x) {
    int r = 1;
    while(x > 0) {
        r = r * 10;
        x--;
    }
    return r;
}

Fixed16 Fixed_pow16(Fixed16 base, Fixed16 exponent) {
    if(Fixed_lt16(exponent, Fixed_zero16)) {
        Fixed16 r = Fixed_div16(Fixed_one16, Fixed_pow16(base, Fixed_neg16(exponent)));
        #ifdef DEBUG
            fprintf(stderr, "Fixed_pow16 (neg): (%f, %f). Result: %f Expected %f\n", Fixed_toFloat16(base), Fixed_toFloat16(exponent), Fixed_toFloat16(r), powf(Fixed_toFloat16(base), Fixed_toFloat16(exponent)));
        #endif
        return r;
    } else {
        Fixed16 r = Fixed_exp16(Fixed_mul16(exponent, Fixed_log16(Fixed_abs16(base))));
        #ifdef DEBUG
            fprintf(stderr, "Fixed_pow16 (pos): (%f, %f). Result: %f Expected %f\n", Fixed_toFloat16(base), Fixed_toFloat16(exponent), Fixed_toFloat16(r), powf(Fixed_toFloat16(base), Fixed_toFloat16(exponent)));
        #endif
        return r;
    }
}

Fixed32 Fixed_pow32(Fixed32 base, Fixed32 exponent) {
    if(Fixed_lt32(exponent, Fixed_zero32)) {
        return Fixed_div32(Fixed_one32, Fixed_pow32(base, Fixed_neg32(exponent)));
    } else {
        return Fixed_exp32(Fixed_mul32(exponent, Fixed_log32(Fixed_abs32(base))));
    }
}
